/*
 * AlteraUsuarioAction.java
 *
 * Created on 12 de Agosto de 2007, 18:27
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.usp.poli.mfc.struts.action;

import br.usp.poli.mfc.struts.DAO.DAOFactory;
import br.usp.poli.mfc.struts.DAO.RevistaDAO;
import br.usp.poli.mfc.struts.DAO.UsuarioDAO;
import br.usp.poli.mfc.struts.actionform.ConsultaRevistaActionForm;
import br.usp.poli.mfc.struts.bean.Usuario;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import br.usp.poli.mfc.struts.actionform.AlteraUsuarioActionForm;

/**
 *
 * @author Edson
 */
public class AlteraUsuarioAction extends Action {
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String ERROR = "error";
    private final static String SUCCESS1 = "success1";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Integer idUsuario = Integer.parseInt(request.getParameter("idUsuario"));
        AlteraUsuarioActionForm frmAltUser = (AlteraUsuarioActionForm) form;
        UsuarioDAO usuarioDAO = DAOFactory.getUsuarioDAO();
        Usuario usuario = new Usuario();
        if(frmAltUser.getNomeUsuario() != null){
            try{
                usuario.setNome(frmAltUser.getNomeUsuario());
                usuario.setLogin(frmAltUser.getLoginUsuario());
                usuario.setId_cargo(Integer.parseInt(frmAltUser.getCargo()));
                usuario.setId_departamento(Integer.parseInt(frmAltUser.getDepartamento()));
                usuario.setSenha(frmAltUser.getPassword());
                if(usuarioDAO.alteraUsuarioByID(usuario))
                    return mapping.findForward(SUCCESS1);
                else
                    return mapping.findForward(ERROR);
            } catch(Exception e){
                e.printStackTrace();
            }
        } else{
            System.out.println("null");
            usuario = usuarioDAO.getUsuarioByID(idUsuario);
            HttpSession ysession = request.getSession();
            ysession.setAttribute("viewRevista",usuario);
            return mapping.findForward(SUCCESS);
        }
        return mapping.findForward(ERROR);
        
    }
}
